/*
 * libc.c - Wrapper per les crides a sistema
 */

#include <libc.h>
#include <string.h>

const char *sys_errlist[] = { "Operation not permitted",
                              "No such file or directory",
                              "No such process",
                              "Interrupted system call",
                              "I/O error",
                              "No such device or address",
                              "Argument list too long",
                              "Exec format error",
                              "Bad file number",
                              "No child processes",
                              "Try again",
                              "Out of memory",
                              "Permission denied",
                              "Bad address",
                              "Block device required",
                              "Device or resource busy",
                              "File exists",
                              "Cross-device link",
                              "No such device",
                              "Not a directory",
                              "Is a directory",
                              "Invalid argument",
                              "File table overflow",
                              "Too many open files",
                              "Not a typewriter",
                              "Text file busy",
                              "File too large",
                              "No space left on device",
                              "Illegal seek",
                              "Read-only file system",
                              "Too many links",
                              "Broken pipe",
                              "Math argument out of domain of func",
                              "Math result not representable",
                              "Resource deadlock would occur",
                              "File name too long",
                              "No record locks available",
                              "Function not implemented",
                              "Directory not empty",
                              "Too many symbolic links encountered",
                              "Operation would block",
                              "No message of desired type",
                              "Identifier removed",
                              "Channel number out of range",
                              "Level 2 not synchronized",
                              "Level 3 halted",
                              "Level 3 reset",
                              "Link number out of range",
                              "Protocol driver not attached",
                              "No CSI structure available",
                              "Level 2 halted",
                              "Invalid exchange",
                              "Invalid request descriptor",
                              "Exchange full",
                              "No anode",
                              "Invalid request code",
                              "Invalid slot",
                              "Resource deadlock would occur",
                              "Bad font file format",
                              "Device not a stream",
                              "No data available",
                              "Timer expired",
                              "Out of streams resources",
                              "Machine is not on the network",
                              "Package not installed",
                              "Object is remote",
                              "Link has been severed",
                              "Advertise error",
                              "Srmount error",
                              "Communication error on send",
                              "Protocol error",
                              "Multihop attempted",
                              "RFS specific error",
                              "Not a data message",
                              "Value too large for defined data type",
                              "Name not unique on network",
                              "File descriptor in bad state",
                              "Remote address changed",
                              "Can not access a needed shared library",
                              "Accessing a corrupted shared library",
                              ".lib section in a.out corrupted",
                              "Attempting to link in too many shared libraries",
                              "Cannot exec a shared library directly",
                              "Illegal byte sequence",
                              "Interrupted system call should be restarted",
                              "Streams pipe error",
                              "Too many users",
                              "Socket operation on non-socket",
                              "Destination address required",
                              "Message too long",
                              "Protocol wrong type for socket",
                              "Protocol not available",
                              "Protocol not supported",
                              "Socket type not supported",
                              "Operation not supported on transport endpoint",
                              "Protocol family not supported",
                              "Address family not supported by protocol",
                              "Address already in use",
                              "Cannot assign requested address",
                              "Network is down",
                              "Network is unreachable",
                              "Network dropped connection because of reset",
                              "Software caused connection abort",
                              "Connection reset by peer",
                              "No buffer space available",
                              "Transport endpoint is already connected",
                              "Transport endpoint is not connected",
                              "Cannot send after transport endpoint shutdown",
                              "Too many references: cannot splice",
                              "Connection timed out",
                              "Connection refused",
                              "Host is down",
                              "No route to host",
                              "Operation already in progress",
                              "Operation now in progress",
                              "Stale NFS file handle",
                              "Structure needs cleaning",
                              "Not a XENIX named type file",
                              "No XENIX semaphores available",
                              "Is a named type file",
                              "Remote I/O error",
                              "Quota exceeded",
                              "No medium found",
                              "Wrong medium type",
                              "Operation Canceled",
                              "Required key not available",
                              "Key has expired",
                              "Key has been revoked",
                              "Key was rejected by service" };


void perror() {
    write(1, "Error: ", 7);
    write(1, (char *)sys_errlist[errno-1], strlen((char *)sys_errlist[errno])-1);
    write(1, "\n", 1);
}

/*SYSCALL #1*/
void exit(void)
{
  __asm__ __volatile (
  "pushl %ebx\n"     //save %ebx because it's of the caller
  "movl $1,%eax\n"  //put 1 in eax (exit code)
  "int $0x80\n "      //generate an interrupt in the port 0x80
  "popl %ebx\n"      //save %ebx restore it's of the caller
  );
}

/*SYSCALL #2*/
int fork()
{
  int result;
  __asm__ __volatile (
  "pushl %%ebx\n"     //save %ebx because it's of the caller
  "movl $2,%%eax\n"  //put 2 in eax (fork code)
  "int $0x80\n "      //generate an interrupt in the port 0x80
  "movl %%eax,%0\n"   //put %eax in result
  "popl %%ebx\n"      //save %ebx restore it's of the caller
  : "=g" (result));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #3*/
int read(int fd,char *buffer,int size)
{
  int result = 0;
  __asm__ __volatile (
		"pushl %%ebx\n"   //save %ebx because it's of the caller
    "movl %1,%%edx\n" //put size in edx
    "movl %2,%%ecx\n" //put buffer in ecx
    "movl %3,%%ebx\n" //put fd in ebx
    "movl $3,%%eax\n" //put 3 in eax (read code)
    "int $0x80\n "    //generate an interrupt in the port 0x80
    "movl %%eax,%0\n" //put %eax in result
		"popl %%ebx\n"    //save %ebx restore it's of the caller
    : "=g" (result)
    : "g" (size) , "g" (buffer) , "g" (fd));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #4*/
int write(int fd,char *buffer,int size)
{
  int result = 0;
  __asm__ __volatile (
		"pushl %%ebx\n"   //save %ebx because it's of the caller
    "movl %1,%%edx\n" //put size in edx
    "movl %2,%%ecx\n" //put buffer in ecx
    "movl %3,%%ebx\n" //put fd in ebx
    "movl $4,%%eax\n" //put 4 in eax (write code)
    "int $0x80\n "    //generate an interrupt in the port 0x80
    "movl %%eax,%0\n" //put %eax in result
		"popl %%ebx\n"    //save %ebx restore it's of the caller
    : "=g" (result)
    : "g" (size) , "g" (buffer) , "g" (fd));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #5*/
int open(const char *path,int flags)
{
  int result = 0;
  __asm__ __volatile (
		"pushl %%ebx\n"   //save %ebx because it's of the caller
    "movl %2,%%ecx\n" //put flag in ecx
    "movl %1,%%ebx\n" //put path in ebx
    "movl $5,%%eax\n" //put 5 in eax (open code)
    "int $0x80\n "    //generate an interrupt in the port 0x80
    "movl %%eax,%0\n" //put %eax in result
		"popl %%ebx\n"    //save %ebx restore it's of the caller
    : "=g" (result)
    : "g" (path) , "g" (flags));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #6*/
int close(int fd)
{
  int result = 0;
  __asm__ __volatile (
		"pushl %%ebx\n"   //save %ebx because it's of the caller
    "movl %1,%%ebx\n" //put fd in ebx
    "movl $6,%%eax\n" //put 6 in eax (open code)
    "int $0x80\n "    //generate an interrupt in the port 0x80
    "movl %%eax,%0\n" //put %eax in result
		"popl %%ebx\n"    //save %ebx restore it's of the caller
    : "=g" (result)
    : "g" (fd));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #10*/
int unlink(const char * path)
{
  int result = 0;
  __asm__ __volatile (
		"pushl %%ebx\n"   //save %ebx because it's of the caller
    "movl %1,%%ebx\n" //put path in ebx
    "movl $10,%%eax\n" //put 10 in eax (open code)
    "int $0x80\n "    //generate an interrupt in the port 0x80
    "movl %%eax,%0\n" //put %eax in result
		"popl %%ebx\n"    //save %ebx restore it's of the caller
    : "=g" (result)
    : "g" (path));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #20*/
int getpid(void)
{
  int result;
  __asm__ __volatile (
  "pushl %%ebx\n"     //save %ebx because it's of the caller
  "movl $20,%%eax\n"  //put 20 in eax (getpid code)
  "int $0x80\n "      //generate an interrupt in the port 0x80
  "movl %%eax,%0\n"   //put %eax in result
  "popl %%ebx\n"      //save %ebx restore it's of the caller
  : "=g" (result));
  return result;
}

/*SYSCALL #21*/
int sem_init(int n_sem, unsigned int value)
{
  int result = 0;
  __asm__ __volatile (
		"pushl %%ebx\n"   //save %ebx because it's of the caller 
    "movl %2,%%ecx\n" //put value in ecx
    "movl %1,%%ebx\n" //put n_sem in ebx
    "movl $21,%%eax\n" //put 21 in eax (sem_init code)
    "int $0x80\n "    //generate an interrupt in the port 0x80
    "movl %%eax,%0\n" //put %eax in result
		"popl %%ebx\n"    //save %ebx restore it's of the caller
    : "=g" (result)
    : "g" (n_sem) , "g" (value));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #22*/
int sem_wait(int n_sem)
{
  int result = 0;
  __asm__ __volatile (
		"pushl %%ebx\n"   //save %ebx because it's of the caller 
    "movl %1,%%ebx\n" //put n_sem in ebx
    "movl $22,%%eax\n" //put 22 in eax (sem_wait code)
    "int $0x80\n "    //generate an interrupt in the port 0x80
    "movl %%eax,%0\n" //put %eax in result
		"popl %%ebx\n"    //save %ebx restore it's of the caller
    : "=g" (result)
    : "g" (n_sem));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #23*/
int sem_signal(int n_sem)
{
  int result = 0;
  __asm__ __volatile (
		"pushl %%ebx\n"   //save %ebx because it's of the caller 
    "movl %1,%%ebx\n" //put n_sem in ebx
    "movl $23,%%eax\n" //put 23 in eax (sem_signal code)
    "int $0x80\n "    //generate an interrupt in the port 0x80
    "movl %%eax,%0\n" //put %eax in result
		"popl %%ebx\n"    //save %ebx restore it's of the caller
    : "=g" (result)
    : "g" (n_sem));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #24*/
int sem_destroy(int n_sem)
{
  int result = 0;
  __asm__ __volatile (
		"pushl %%ebx\n"   //save %ebx because it's of the caller 
    "movl %1,%%ebx\n" //put n_sem in ebx
    "movl $24,%%eax\n" //put 24 in eax (sem_destroy code)
    "int $0x80\n "    //generate an interrupt in the port 0x80
    "movl %%eax,%0\n" //put %eax in result
		"popl %%ebx\n"    //save %ebx restore it's of the caller
    : "=g" (result)
    : "g" (n_sem));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #34*/
int nice(int quantum)
{
  int result;
  __asm__ __volatile (
  "pushl %%ebx\n"  
  "movl %1,%%ebx\n"   //save %ebx because it's of the caller
  "movl $34,%%eax\n"  //put 2 in eax (fork code)
  "int $0x80\n "      //generate an interrupt in the port 0x80
  "movl %%eax,%0\n"   //put %eax in result
  "popl %%ebx\n"      //save %ebx restore it's of the caller
  : "=g" (result)
  :"g" (quantum));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #35*/
int get_stats(int pid, struct stats * st)
{
  int result = 0;
  __asm__ __volatile (
		"pushl %%ebx\n"   //save %ebx because it's of the caller 
    "movl %2,%%ecx\n" //put st in ecx
    "movl %1,%%ebx\n" //put pid in ebx
    "movl $35,%%eax\n" //put 35 in eax (get_stats code)
    "int $0x80\n "    //generate an interrupt in the port 0x80
    "movl %%eax,%0\n" //put %eax in result
		"popl %%ebx\n"    //save %ebx restore it's of the caller
    : "=g" (result)
    : "g" (pid) , "g" (st));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}

/*SYSCALL #41*/
int dup(int fd)
{
  int result = 0;
  __asm__ __volatile (
		"pushl %%ebx\n"   //save %ebx because it's of the caller 
    "movl %1,%%ebx\n" //put st in ebx
    "movl $41,%%eax\n" //put 41 in eax (get_stats code)
    "int $0x80\n "    //generate an interrupt in the port 0x80
    "movl %%eax,%0\n" //put %eax in result
		"popl %%ebx\n"    //save %ebx restore it's of the caller
    : "=g" (result)
    : "g" (fd));
  if (result < 0) 
	{
		errno = -result;
		result = -1;
	}
	return result;
}


